/*
 * 2006-03-23 李希明 创建。
 */
package com.hd123.rumba.util;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.UserType;

/**
 * 自定义类型Version的Hibernate映射类型。
 * 
 * @author lxm
 * 
 */
public class VersionType implements UserType {

  private static final int[] SQL_TYPES = {
    Types.VARCHAR };

  public int[] sqlTypes() {
    return SQL_TYPES;
  }

  public Class returnedClass() {
    return Version.class;
  }

  public boolean equals(Object arg0, Object arg1) throws HibernateException {
    if (arg0 == arg1)
      return true;
    if (arg0 == null || arg1 == null)
      return false;
    return ((Version) arg0).equals((Version) arg1);
  }

  public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException,
      SQLException {
    if (resultSet.wasNull()) return null;
    return new Version(resultSet.getString(names[0]));
  }

  public void nullSafeSet(PreparedStatement statement, Object value, int index) throws HibernateException,
      SQLException {
    if (value == null) {
      statement.setNull(index, Types.VARCHAR);
    } else {
      statement.setString(index, ((Version) value).toString());
    }
  }

  public Object deepCopy(Object arg0) throws HibernateException {
    return ((Version) arg0).clone();
  }

  public boolean isMutable() {
    return false;
  }

}
